package com.bdb.myswipemultitype.swipeListview_v2;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bdb.myswipemultitype.R;

import me.drakeet.multitype.ItemViewProvider;

import static android.widget.Toast.LENGTH_SHORT;

/**
 * Created by zinc on 2017/5/31.
 */

public abstract class SwipeFrameBinder<Content extends SwipeContent, SubViewHolder extends ContentHolder>
        extends ItemViewProvider<SwipeContent, SwipeFrameBinder.FrameHolder> {

    protected abstract ContentHolder onCreateContentViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent);

    protected abstract void onBindContentViewHolder(@NonNull SubViewHolder holder, @NonNull Content content);

    @NonNull
    @Override
    protected FrameHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.swipe_v2_item_layout, parent, false);
        ContentHolder subViewHolder = onCreateContentViewHolder(inflater, parent);
        return new FrameHolder(root, subViewHolder, this);
    }


    @Override
    @SuppressWarnings("unchecked")
    protected void onBindViewHolder(@NonNull FrameHolder holder, @NonNull SwipeContent swipeContent) {
        holder.right_menu.setText("test");
        onBindContentViewHolder((SubViewHolder) holder.subViewHolder, (Content) swipeContent);
    }

    static class FrameHolder extends RecyclerView.ViewHolder {

        private TextView right_menu;
        private FrameLayout container;
        private ContentHolder subViewHolder;

        FrameHolder(View itemView, final ContentHolder subViewHolder, final SwipeFrameBinder binder) {
            super(itemView);
            right_menu = (TextView) findViewById(R.id.right_menu);
            container.addView(subViewHolder.itemView);
            this.subViewHolder = subViewHolder;
            this.subViewHolder.frameHolder = this;

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    Toast.makeText(v.getContext(),
                            "Position: " + getAdapterPosition(), LENGTH_SHORT).show();
                }
            });
            right_menu.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
//                    binder.getAdapter().getItems().getItemId(getAdapterPosition()).;
                    binder.getAdapter().notifyItemRemoved(getAdapterPosition());
                }
            });
        }

        private View findViewById(int resId) {
            return itemView.findViewById(resId);
        }
    }

}
